package co.gov.medellin.ssti.sub.web.action;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import co.gov.medellin.ssti.sub.model.service.EmpresaService;
import co.gov.medellin.ssti.sub.model.vo.BeneficioOtorgadoEmpresaVO;
import co.gov.medellin.ssti.sub.model.vo.CondicionConsultaVO;
import co.gov.medellin.ssti.sub.model.vo.RepAtributoVO;
import co.gov.medellin.ssti.sub.model.vo.RepEmpresaVO;
import co.gov.medellin.ssti.sub.web.form.RepDetEmpresaForm;

@Component
public class SelectGeneradoDetEmpresaAction extends Action {
	@Autowired
	EmpresaService empresaServiceImpl;

	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	@SuppressWarnings("unchecked")
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		RepDetEmpresaForm formBean = (RepDetEmpresaForm) form;
		HttpSession session = request.getSession();

		RepEmpresaVO repDetEmpresaVO = (RepEmpresaVO) session
				.getAttribute("criteriaRepDetEmpresa");

		List<RepAtributoVO> lEspecifico = new LinkedList<RepAtributoVO>();

		lEspecifico = (List<RepAtributoVO>) session
				.getAttribute("listaAtrEspecificosRepDetEmpresa");
		List<CondicionConsultaVO> lFiltro = (List<CondicionConsultaVO>) session
				.getAttribute("filtrosRepDetEmpresa");

		String codigoAtrGestor = (String) session
				.getAttribute("codigoAtrGestor");
		String codigovfl = (String) session.getAttribute("codigovfl");

		int itamanoEsp = lEspecifico.size();
		String tamanoEsp = String.valueOf(itamanoEsp);

		session.setAttribute("TamanoEsp", tamanoEsp);

		// RepDetBOTVO criterea = new RepDetBOTVO();
		// Long subpro = formBean.getRepEmpresaVO().getCodeSubprograma();
		// String actividadEconomica = formBean.getRepEmpresaVO()
		// .getActividadEconomica();
		// Long codeEmpresa = formBean.getRepEmpresaVO().getCodeEmpresa();
		// String fechaInicial = formBean.getFechaIni();
		// String fechaFinal = formBean.getFechaFin();
		// formBean.getRepEmpresaVO().setDesde(fechaInicial);
		// formBean.getRepEmpresaVO().setHasta(fechaFinal);

		// if (codigoAtrGestor != null && codigovfl != null) {
		// criterea.setCodigoAtrGestor(codigoAtrGestor);
		// criterea.setGestor(codigovfl);
		// }

		// consultar benef.
		Collection<BeneficioOtorgadoEmpresaVO> beneficios;
		beneficios = empresaServiceImpl.findReporteBeneficioOtorgadoDet(
				lEspecifico, lFiltro, repDetEmpresaVO);
		formBean.setBeneficios(beneficios);
		session.setAttribute("beneficiosRepDetEmpresa", beneficios);
		session.setAttribute("lEspecificosRepDetEmpresa",
				formBean.getlEspecificos());
		Iterator<BeneficioOtorgadoEmpresaVO> it = formBean.getBeneficios()
				.iterator();
		while (it.hasNext()) {
			BeneficioOtorgadoEmpresaVO vo = it.next();
			vo.getCampos();
		}

		// if (formBean.getlEspecificos() != null) {
		// for (RepAtributoVO vo : formBean.getlEspecificos()) {
		// System.out.println("etiqueta: " + vo.getEtiqueta());
		// }
		// }
		/*
		 * try {
		 * 
		 * criterea.setCantidadBeneficiarios(repDetBOTService
		 * .findCantidadBeneficiarios(lFiltro, criterea));
		 * criterea.setValor(repDetBOTService.findValorTotal(lFiltro,
		 * criterea)); session.setAttribute("Parametros", criterea);
		 * List<ReporteDetVO> reporte = (List<ReporteDetVO>) repDetBOTService
		 * .findReporte(lGeneral, lEspecifico, lFiltro, criterea);
		 * criterea.setEntregas(new Integer(reporte.size())); // TODO ???
		 * repDetBOTService.findFilters(criterea);
		 * 
		 * session.setAttribute("listareporte", reporte); } catch (DAOException
		 * e) { SUBException.logDebugError("", e); }
		 */

		return mapping.findForward("success");
	}
}
